using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Windows.Forms;

using HTML;

namespace PointBridge.SPDX
{
    public class Program
    {
        static void Main(string[] args)
        {
            int numOfArgs = args.GetLength(0);
            if (numOfArgs != 2)
            {
                Console.WriteLine("Usage: SPDXCmd.exe <site-collection-url> <site-directory-name>");
                return;
            }
            string siteStr = args[0];
            string webStr = args[1];

            try
            {
                using (SPSite site = new SPSite(siteStr))
                {
                    using (SPWeb web = site.OpenWeb(webStr))
                    {
                        SPDXConfig config = new SPDXConfig(GetItem(web));
                        OptionsSet availableContentTypes = new OptionsSet(new LAHTMLParser(config.contentTypesXml));
                        OptionsSet availableLists = new OptionsSet(new LAHTMLParser(config.listsXml));
                        
                        CodeGen codeGen = new CodeGen();
                        Dictionary<string, bool> classes = new Dictionary<string, bool>();

                        int count = 0;

                        //generate content type wrappers
                        for (int i = 0; i < availableContentTypes.count; i++)
                        {
                            if (availableContentTypes[i].optionValue == true)
                            {
                                string className = CodeGen.ConvertSPNameToCS(availableContentTypes[i].optionName);
                                if (classes.ContainsKey(className)) throw new PointBridgeEx(string.Format("This setup results in two classes with duplicate name ({0}).", className));
                                classes.Add(className, true);
                                codeGen.GenarateContentTypeWrapper(web.AvailableContentTypes[availableContentTypes[i].optionName], config, true);
                                count++;
                            }
                        }

                        //generate list wrappers
                        for (int i = 0; i < availableLists.count; i++)
                        {
                            if (availableLists[i].optionValue == true)
                            {
                                string className = CodeGen.ConvertSPNameToCS(availableLists[i].optionName + "ListItem");
                                if (classes.ContainsKey(className)) throw new PointBridgeEx(string.Format("This setup results in two classes with duplicate name ({0}).", className));
                                classes.Add(className, true);
                                codeGen.GenerateListItemWrapper(web.Lists[availableLists[i].optionName], config, true);
                                count++;

                            }
                        }

                        //generate Container Classes
                        CodeGen.GenerateStaticClasses(config, "ContainerClasses", true);
                        Console.WriteLine(count + " requested wrapper(s) successfully generated!");
                    }
                }
            }
            catch(Exception ex)
            {
                Console.Beep();
                string errorMsg = "SPDX: Code Generation Failed: " + ex.Message;
                Console.OpenStandardError();
                Console.WriteLine(errorMsg + Environment.NewLine);
                MessageBox.Show(errorMsg);
                Environment.ExitCode = -1;
            }
        }

        public static SPListItem GetItem(SPWeb web)
        {
            SPListItem item;
            SPList list;
            try
            {
                list = web.Lists["SPDXConfig_" + web.ID];
            }
            catch (Exception ex)
            {
                throw new PointBridgeEx("Unable to find SPDXConfig list: " + ex.Message);
            }
            if (list.Items.Count == 0) throw new PointBridgeEx("You must generate code using web interface once before using SPDXcmd.exe utility.");
            else item = list.Items[0];
            return item;
        }
    }
}
